home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / PROGENV / MethodAccessor.C < prev    next >
C/C++ Source or Header  |  1992-05-21  |  4KB  |  156 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "MethodAccessor.h"
  6.  
  7. #include "Class.h"
  8. #include "OrdColl.h"
  9. #include "PathLookup.h"
  10. #include "String.h"
  11. #include "System.h"
  12. #include "CheapText.h"
  13. #include "CodeTextView.h"
  14.  
  15. #include "EtPeManager.h"
  16.  
  17. const int cMaxName= 400;
  18.  
  19. //---- PeMethodAccessor ----------------------------------------------------
  20.  
  21. PeMethodAccessor::PeMethodAccessor()
  22. {
  23. }
  24.  
  25. class Collection *PeMethodAccessor::FindMethods(Class*, PathLookup*)
  26. {
  27.     AbstractMethod("FindMethods");
  28.     return 0;
  29. }
  30.  
  31. //---- PeMapMethodAccessor ----------------------------------------------------
  32.  
  33. static char *dtor= "_dtor",
  34.         *ctor= "_ctor";
  35.  
  36. PeMapMethodAccessor::PeMapMethodAccessor()
  37. {
  38.     mapSrcPath= 0;
  39. }
  40.  
  41. PeMapMethodAccessor::~PeMapMethodAccessor()
  42. {
  43.     SafeDelete(mapSrcPath);
  44. }
  45.  
  46. void PeMapMethodAccessor::CreateMapSearchPath(PathLookup *universe)
  47. {
  48.     char *p, path[2000];
  49.  
  50.     strcpy(path, ".MAP");
  51.     PathIter next(universe);
  52.     while (p= next()) 
  53.     strcat(path, form(":%s/.MAP:%s", p, p));
  54.     mapSrcPath= new PathLookup(path);
  55. }
  56.  
  57. char *PeMapMethodAccessor::GetMapFileName(Class *cl, PathLookup *universe)
  58. {
  59.     char mapname[cMaxName], fname[cMaxName], *bp;
  60.     static char mappathname[2*cMaxName];
  61.     
  62.     const char *path, *base;
  63.     if (mapSrcPath == 0) 
  64.     CreateMapSearchPath(universe);
  65.     
  66.     fname[0]= mapname[0]= '\0';
  67.     base= cl->GetImplFileName();
  68.     path= ""; /* cl->GetCompDir(); */
  69.     bp= strrchr(base, '/');
  70.     if (bp == 0)
  71.     bp= (char*)base;
  72.     sprintf(mapname, "%s.map", bp);
  73.     sprintf(mappathname, "%s/%s", path, mapname); 
  74.     if(gSystem->AccessPathName(mappathname, 4) != 0) {
  75.     sprintf(mappathname, "%s/.MAP/%s", path, mapname); 
  76.     if(gSystem->AccessPathName(mappathname, 4) != 0)
  77.         if (!mapSrcPath->Lookup(mapname, mappathname)) 
  78.         return 0;
  79.     } 
  80.     return mappathname; 
  81. }
  82.  
  83. Collection *PeMapMethodAccessor::FindMethods(Class *cl, PathLookup *universe)
  84. {
  85.     char classname[cMaxName], methodname[cMaxName], clname[cMaxName], *name;
  86.     int line, pub;
  87.     OrdCollection *oc= new OrdCollection;
  88.     char *mapfile= GetMapFileName(cl, universe);
  89.     if (mapfile == 0)
  90.     return (Collection*) oc;
  91.     IStream fp(mapfile);
  92.     
  93.     if (fp) {
  94.     // assumption: method is translated to _class_method
  95.     sprintf(clname, "_%s_", cl->Name());
  96.     while (fp >> classname >> methodname >> line >> pub) {
  97.         if (strncmp(methodname, clname, strlen(clname)) == 0) {
  98.         // change _ctor and & _dtor
  99.         name= methodname+strlen(clname);
  100.         if (strncmp(name, dtor, strlen(dtor)) == 0)
  101.             name= form("~%s", cl->Name());
  102.         else if (strncmp(name, ctor, strlen(ctor)) == 0)
  103.             name= form("+%s(%s)", cl->Name(), name+strlen(ctor));
  104.         oc->Add(new PeMethodReference(line, cl, name, pub == 2));
  105.         }
  106.     }
  107.     }
  108.     return (Collection *)oc; // ??? cast to keep cfront 1.2 happy 
  109. }
  110.  
  111. //---- MethodScanner -----------------------------------------------------------
  112.  
  113. class MethodScanner: public CodeAnalyzer {
  114.     Collection *cp;
  115.     char *cname;
  116.     Class *clp;
  117. protected:
  118.     void Function(int line, int start, int end, char *name, char *classname);
  119. public:
  120.     MethodScanner(Text *t, Collection *cp, Class *clp);
  121. };
  122.  
  123. MethodScanner::MethodScanner(Text *t, Collection *c, Class *classp)
  124.                                 : CodeAnalyzer(t)
  125. {
  126.     cp= c; 
  127.     clp= classp;
  128.     cname= clp->Name();
  129. }
  130.  
  131. void MethodScanner::Function(int line, int, int, char *name, char *clname)
  132. {
  133.     if (clname && strcmp(clname, cname) == 0) 
  134.     cp->Add(new PeMethodReference(line, clp, name, TRUE));
  135. }
  136.  
  137. //---- PeScanMethodAccessor ----------------------------------------------------
  138.  
  139. PeScanMethodAccessor::PeScanMethodAccessor()
  140. {
  141. }
  142.  
  143. Collection *PeScanMethodAccessor::FindMethods(Class *cl, PathLookup *)
  144. {
  145.     OrdCollection *oc= new OrdCollection;
  146.     char filename[1000];
  147.     if (!gEtPeManager->FileOfClass(cl, filename, FALSE))
  148.     return 0;
  149.     IStream fp(filename);
  150.     CheapText text(5000);
  151.     text.ReadFromAsPureText(fp);  
  152.     MethodScanner ms(&text, oc, cl);
  153.     ms.Doit();
  154.     return (Collection*)oc; // ??? cast to keep cfront 1.2 happy
  155. }
  156.